# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from JT_8103 import JT_8103

my_class=JT_8103()
class JT_0107:
    result = []
    analysisDescription=['终端类型:','制造商ID:','终端型号:','终端ID:','终端SIM卡ICCID:','终端硬件版本号长度:','终端硬件版本号:',
                         '终端固件版本号长度:','终端固件版本号:','GNSS模块属性:','通信模块属性:']
    analysis0107Length=[2,5,20,7,10,1,0,1,0,1,1]
    #终端类型
    term_type_list=['客运车辆','危险品车辆','普通货运车辆','出租车辆','','','硬盘录像']
    # GNSS模块属性
    gnss_model = ['GPS定位', '北斗定位', 'GLONASS定位', 'Galileo定位']
    #通信模块属性
    cmu_model =['GPRS通信','CDMA通信','TD-SCDMA通信','WCDMA通信','CDMA2000通信','TD-LTE通信','','其他通信方式']
    def LogicAisle(self, protocolStr, header):
        count=0
        for i in range(len(self.analysisDescription)):
            s=protocolStr[count:count+self.analysis0107Length[i]*2]
            #终端类型
            if i==0:
                term_type=s
                term_type=my_class.hex_to_bin(term_type,self.analysis0107Length[i]*8)
                header.append(self.analysisDescription[i]+term_type+"\n")
                self.set_term_type(term_type,header)
            #制造商ID，终端型号，终端ID，终端SIM卡ICCID,版本号
            elif i==1 or i==2 or i==3 or i==6 or i==8:
                s=s.decode("hex").decode('gb2312')
                header.append(self.analysisDescription[i]+s+"\n")
            #版本号长度
            elif i==5 or i==7:
                s=int(s,16)
                self.analysis0107Length[i+1]=s
                header.append(self.analysisDescription[i]+str(s)+"\n")
            #GNSS模块属性
            elif i==9:
                s=my_class.hex_to_bin(s,self.analysis0107Length[i]*8)
                header.append(self.analysisDescription[i]+s+"\n")
                self.set_gnssmodel(s,header)
            #通信模块属性
            elif i==10:
                s = my_class.hex_to_bin(s, self.analysis0107Length[i] * 8)
                header.append(self.analysisDescription[i] + s + "\n")
                self.set_cmumodel(s, header)
            elif i==4:
                header.append(self.analysisDescription[i]+ s +"\n")

            count +=self.analysis0107Length[i]*2
        self.result=header

    def set_term_type(self,s,header):
        count=16
        for i in range(count):
            msg=""
            term_bit=s[count-1:count]
            if term_bit=="1"and i<4:
                msg="适用"
                header.append(msg + self.term_type_list[i] + "\n")
            elif term_bit=="1"and i==6:
                msg="支持"
                header.append(msg+ self.term_type_list[i]  + "\n")
            elif term_bit=="0"and i<4:
                msg="不适用"
                header.append(msg + self.term_type_list[i] + "\n")
            elif term_bit=="0"and i==6:
                msg="不支持"
                header.append(msg + self.term_type_list[i] + "\n")
            elif i==7and term_bit=="1":
                msg="分体机"
                header.append(msg + "\n")
            elif i==7and term_bit=="0":
                msg="一体机"
                header.append(msg + "\n")
            count -=1

    def set_gnssmodel(self,s,header):
        count = 8
        for i in range(count):
            if i > 3:
                pass
            else:
                gnss_bit = int(s[count - 1:count])
                if gnss_bit == 1:
                    header.append("支持" + self.gnss_model[i] + "\n")
                else:
                    header.append("不支持" + self.gnss_model[i] + "\n")
            count -= 1

    def set_cmumodel(self,s,header):
        count = 8
        for i in range(count):
            cmu_bit = int(s[count - 1:count])
            if i==6:
                pass
            else:
                if cmu_bit == 1:
                    header.append("支持" + self.cmu_model[i] + "\n")
                else:
                    header.append("不支持" + self.cmu_model[i] + "\n")
            count -= 1

